<template>
    <div class="detail">
      <van-nav-bar
        title="详情页"
        left-text="返回"
        right-text="..."
        left-arrow
        @click-left="$router.go(-1)"
      />
      <div class="shop-header" :style="{backgroundImage:'url(' + (shopinfo.info && shopinfo.info.poi_back_pic_url) +')' }">
        <div class="left">
          <img :src="shopinfo.info && shopinfo.info.picUrl" alt="">
          <span>{{ shopinfo.info && shopinfo.info.name}}</span>
        </div>
        <div class="right" v-if="isCollect" @click="request_unfav(shopinfo.info)" style="color:red">❤</div>
        <div class="right" v-else @click="request_fav(shopinfo.info)">❤</div>
      </div>

      <div class="box">
        <div class="left">
          <van-sidebar v-model="currentIndex">
            <van-sidebar-item  v-for="(item,index) in shopinfo.taglist" :key="index" :title="item.name" />
          </van-sidebar>
        </div>
        <div class="right">
          <div class="good" v-for="(item,index) in shopinfo.taglist && shopinfo.taglist[currentIndex].goodlist" :key="index">
            <div class="imgbox">
              <img :src="item.picture" alt="">
            </div>
            <div class="text">
              <div class="up">
                <div class="name">{{item.name}}</div>
                <div class="desc">{{item.month_saled_content}}</div>
              </div>
              <div class="down">
                <div class="price">¥{{item.min_price}}</div>
                <div class="opes">
                  <span v-show="isInShopcart(item)" @click="request_shopcart_sub(item)">-</span>
                  <span v-show="isInShopcart(item)">{{isInShopcartcount(item)}}</span>
                  <span @click="request_shopcart_add(item)">+</span>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
      <ShopCart ref="ShopCart"/>
      <!-- 提交栏 -->
      <div class="summary">
        <span><van-icon name="cart" size="24" @click="$refs.ShopCart.changeVisible()"/>总数:{{totalCount}} 总价:{{totalPrice}} </span>
        <span>去结算</span>
      </div>
    </div>
  </template>
  
  <script>
  import { mapState,mapMutations,mapGetters,mapActions } from 'vuex'
  import ShopCart from '../components/ShopCart.vue'
  import { Dialog } from 'vant';
  import { shop_shopinfo } from '../utils/api'
  export default {
    data(){
      return {
        currentIndex:0,
        shopinfo:{}
      }
    },
    components:{
      ShopCart
    },
    computed:{
      ...mapState(['collectlist','shopcartlist']),
      ...mapGetters([]),
      isCollect(){ 
        var index = this.collectlist.findIndex((item)=>{
          return item.mtWmPoiId == this.shopinfo.mtWmPoiId;
        })
        return index !== -1
      },
      //总数
      totalCount(){
        var sum = 0;
        this.shopcartlist.forEach((item)=>{
          sum += item.count;
        })
        return sum;
      },
      // 计算总价
      totalPrice(){
        var sum = 0;
        this.shopcartlist.forEach((item)=>{
          sum += item.count/1*item.min_price/1;
        })
        return sum.toFixed(2);
      }
    },
    methods:{
      
      ...mapMutations(['collect','cancel_collect']),
      ...mapActions(['request_fav','request_unfav','request_shopcart_add','request_shopcart_sub']),
      //判断当前商品是否在购物车中
      isInShopcart(item){
        var index=this.shopcartlist.findIndex((it)=>{
          return it.id == item.id
        })
        return index !== -1;
      },
      //当前商品在购物车中的数量
      isInShopcartcount(item){
        var good=this.shopcartlist.find((it)=>{
          return it.id == item.id
        })
        if(good){
          return good.count;
        }else{
          return 0;
        }
      },

    },
    mounted(){
      shop_shopinfo( {id: this.$route.params.id} ).then((res)=>{
        if( res.data.shopinfo ){ 
          this.shopinfo = res.data.shopinfo;
        }else{ 
          Dialog.alert({
            title: '提示',
            message: '该店铺已倒闭!'
          }).then(() => {
            this.$router.go(-1);
          });
        }
      })
    }
  }
  </script>
  
  <style lang='scss' scoped>
  .shop-header{
    display: flex;
    justify-content: space-between;
    padding: 50px 10px;
    background-size: cover;
    background-repeat: no-repeat;
    color: white;
    .left{
      flex:1;
      display: flex;
      align-items: center;
      img{
        width: 30px;
        margin-right: 10px;
        border-radius: 50%;
      }
    }
    .right{
      width: 20px;
      text-align: right;
    }
  }
  .box{
    display: flex;
    .left{
      width: 80px;
    }
    .right{
      flex:1;
      .good{
        display: flex;
        margin: 10px;
        .imgbox{
          width: 80px;
          margin-right: 10px;
          img{
            width: 100%;
          }
        }
        .text{
          flex:1;
          font-size: 12px;
          display: flex;
          flex-direction: column;
          justify-content: space-between;
          .down{
            display: flex;
            justify-content: space-between;
            span{
              display: inline-block;
              width: 16px;
              height: 16px;
              background-color: lightskyblue;
              color: white;
              font-size: 12px;
              text-align: center;
              line-height: 16px;
              margin-right: 10px;
              border-radius: 50%;
            }
          }
        }
      }
    }
  }
  // 提交栏
  .summary {
    display: flex;
    justify-content: space-between;
    align-items: center;
    background-color: pink;
    color: black;
    left: 0;
    position: fixed;
    bottom: 0;
    width: 100%;
    padding: 10px;
    box-sizing: border-box;
}

.summary span:nth-child(2) {
    background-color: lightgreen;
    color: black;
    padding: 5px 15px;
    border-radius: 5px;
}
  </style>